﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.logGetOil
{
    public partial class Edit : EditPagebase<IGetOilContract, GetOil>
    {
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }
        public IOilSerialNumContract OilSerialNumContract { get; set; }
        public IMoneyChangeContract MoneyChangeContract { get; set; }
        public IUserContract UserContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<MoneyChange>(MoneyChangeContract.GetResolvedTree(), 0L, ed_MoneyChange);
                //FineUIListHelper.BindDDL(MoneyChangeContract.EntityName, this.MoneyChangeContract.GetSelectList(), 0L, ed_MoneyChange);
                //FineUIListHelper.BindDDL<User>(UserContract.GetResolvedTree(), 0L, ed_User);
                //FineUIListHelper.BindDDL(UserContract.EntityName, this.UserContract.GetSelectList(), 0L, ed_User);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(GetOil entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_OilPort.SetValue(entity.OilPort);
            ed_OperateType.SetValue(entity.OperateType);
            ed_OpreateTime.SetValue(entity.OpreateTime);
            ed_OilOutPut.SetValue(entity.OilOutPut);
            ed_OilBefore.SetValue(entity.OilBefore);
            ed_OilAfter.SetValue(entity.OilAfter);
            ed_Price.SetValue(entity.Price);
            ed_Money.SetValue(entity.Money);
            ed_LinkEntityId.SetValue(entity.LinkEntityId);
            ed_MoneyBefore.SetValue(entity.MoneyBefore);
            ed_MoneyAfter.SetValue(entity.MoneyAfter);
            if (!entity.MoneyChangeId.IsNull())
                ed_MoneyChange.SetValue(entity.MoneyChangeId);
            if (!entity.CustomerId.IsNull())
                ed_Customer.SetValue(entity.CustomerId);
        }

        protected override OperationResult SaveData(GetOil entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            entity.OilPort = ed_OilPort.GetValue().ToInt();
            entity.OperateType = ed_OperateType.GetValue().ToInt();
            entity.OpreateTime = ed_OpreateTime.GetValue().ToNullableDateTime();
            if(entity.OpreateTime ==DateTime.MinValue)
                entity.OpreateTime = null;
            entity.OilOutPut = ed_OilOutPut.GetValue().ToDouble();
            entity.OilBefore = ed_OilBefore.GetValue().ToDouble();
            entity.OilAfter = ed_OilAfter.GetValue().ToDouble();
            entity.Price = ed_Price.GetValue().ToDouble();
            entity.Money = ed_Money.GetValue().ToDouble();
            entity.LinkEntityId = ed_LinkEntityId.GetValue().ToLong();
            entity.MoneyBefore = ed_MoneyBefore.GetValue().ToDouble();
            entity.MoneyAfter = ed_MoneyAfter.GetValue().ToDouble();
            if (ed_MoneyChange.GetValue().ToLong() == 0)
                entity.MoneyChangeId = null;
            else
                entity.MoneyChangeId = ed_MoneyChange.GetValue().ToLong();
            if (ed_Customer.GetValue().ToLong() == 0)
                entity.CustomerId = null;
            else
                entity.CustomerId = ed_Customer.GetValue().ToLong();
            return base.SaveData(entity);
            
        }
    }
}
